package com.morgan.recyclerviewdemo

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.morgan.recyclerviewdemo.adapter.MainAdapter
import com.morgan.recyclerviewdemo.entity.IClassificateTag
import com.morgan.recyclerviewdemo.entity.TextIconBean
import com.morgan.recyclerviewdemo.itemdecoration.*
import com.morgan.recyclerviewdemo.utils.DeviceUtil
import com.morgan.recyclerviewdemo.utils.SortUtils
import com.morgan.recyclerviewdemo.utils.setVisible
import com.morgan.recyclerviewdemo.view.VerticalNavigationBar
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val dp10 = DeviceUtil.dp2px(this, 10f)
        val manager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        recyclerview.layoutManager = manager
//        recyclerview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true)

//        recyclerview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
//        recyclerview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true)
//        recyclerview.addItemDecoration(LinearItemDecoration
////            .Builder(RecyclerView.VERTICAL)
//            .Builder(RecyclerView.HORIZONTAL)
////            .size(dp10)
////            .margin(dp10, dp10, dp10, dp10)
////            .size(DeviceUtil.dp2px(this, 1f))
//            .color(Color.RED)
//            .drawable(ContextCompat.getDrawable(this, R.mipmap.mylauncher)!!)
//            .build())


//        recyclerview.addItemDecoration(LinearItemDecoration
////            .Builder(RecyclerView.VERTICAL)
//            .Builder(RecyclerView.HORIZONTAL)
//            .size(dp10)
////            .margin(dp10, dp10, dp10, dp10)
////            .size(DeviceUtil.dp2px(this, 1f))
//            .color(Color.RED)
////            .drawable(ContextCompat.getDrawable(this, R.mipmap.ic_launcher)!!)
//            .build())



//        recyclerview.layoutManager = GridLayoutManager(this, 3, RecyclerView.HORIZONTAL, false)
//        recyclerview.layoutManager = GridLayoutManager(this, 3, RecyclerView.VERTICAL, false)
//
//        val customDrawable = ContextCompat.getDrawable(this, R.drawable.custom_drawbale)
////        customDrawable?.setBounds(0, 0, dp10, dp10)
//        recyclerview.addItemDecoration(
//            DividerGridItemDecoration(
//                customDrawable,
//                this,
//                3
//            )
//        )

        val list = mutableListOf<TextIconBean>()
        val cities = resources.getStringArray(R.array.provinces)
        val rang = 0..cities.size - 1
//        val rang = 0..8
        for (i in rang) {
            list.add(
                TextIconBean(
                    cities[i],
//                    "$i",
                    R.mipmap.ic_launcher
                )
            )
        }

        SortUtils.sort(list)
        list[0].mNeedShowHoverTop = false
//        recyclerview.layoutManager = GridLayoutManager(applicationContext, 3)
//        recyclerview.addItemDecoration(DividerGridItemDecoration(applicationContext, 3))
//        recyclerview.addItemDecoration(DividerItemDecoration(applicationContext, DividerItemDecoration.VERTICAL))
//        recyclerview.addItemDecoration(SimpleGrideItemDecoration(applicationContext))
        recyclerview.addItemDecoration(ClassificationItemDecoration(this, list))
//        recyclerview.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST))
//        val dp1 = DeviceUtil.dp2px(this, 1f)
//        recyclerview.addItemDecoration(LinearItemDecoration.Builder(RecyclerView.VERTICAL)
//            .size(dp1)
////            .margin(dp10, dp10, dp10, dp10)
////            .size(DeviceUtil.dp2px(this, 1f))
//            .color(Color.RED)
////            .drawable(ContextCompat.getDrawable(this, R.mipmap.ic_launcher)!!)
//            .build())
        vnb.setUpWith(list, object : VerticalNavigationBar.OnPressMoveListener {
            override fun onPress(letter: IClassificateTag, position: Int) {
                tv_center.setVisible(true)
                tv_center.text = letter.getTag()
                manager.scrollToPositionWithOffset(position, 0)
            }

            override fun onPressEnd() {
                tv_center.setVisible(false)
            }

        })
        val adpter : MainAdapter = MainAdapter()
        adpter.addData(list)
        recyclerview.adapter = adpter
    }
}